// select + channel
package main

import (
	"fmt"
	"time"
)

func main() {
	ch1 := make(chan int)
	ch2 := make(chan int)

	go func() {
		for {
			go func() {
				fmt.Println(Now(), " ch1 发送中")
				ch1 <- 11
				fmt.Println(Now(), " ch1 已发送")
			}()

			go func() {
				fmt.Println(Now(), " ch2 发送中")
				ch2 <- 22
				fmt.Println(Now(), " ch2 已发送")
			}()

			time.Sleep(1 * time.Second)
		}
	}()

	for {
		fmt.Println(Now(), " select start")
		// 随机挑选case
		select {
		case data := <-ch1:
			fmt.Println(Now(), " select ch1接收的数据:", data)
		case data := <-ch2:
			fmt.Println(Now(), " select ch2接收的数据:", data)
		}
		fmt.Println(Now(), " select end")

		//time.Sleep(1 * time.Second)
	}

}

func Now() string {
	var now = time.Now()
	return now.Format("2006-01-02 15:04:05")
}
